
package com.qf.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.qf.dao.ContactDao;
import com.qf.model.Contact;
import com.qf.utils.C3P0Util;
import com.sun.org.apache.regexp.internal.recompile;

public class ContactDaoImpl implements ContactDao {

	@Override
	public boolean addContact(String sql, Object[] params) {
		int i=0;
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		try {
			i=qr.update(sql,params);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		if(i==1){
			return true;
		}
		
		return false;
			
	}
	@Override
	public Contact findContact(String sql, Object[] params) {
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		Contact c=null;
		try {
			c=qr.query(sql, new BeanHandler<>(Contact.class),params);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		//TODO
		return c;
			
	}
	@Override
	public boolean upContact(String sql, Object[] params) {
		int i=0;
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		try {
			i = qr.update(sql,params);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		if(i==1){
			return true;
		}
		return false;
			
	}
	@Override
	public List<Contact> findContacts(String sql, Object[] params) {
		QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
		List<Contact> c=null;
		try {
			c=qr.query(sql, new BeanListHandler<>(Contact.class),params);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		//TODO
		return c;
			
	}


}

	